$(function(){
    //获取到cookie
    var cookieName = $.cookie('username')
    var cookieImg = $.cookie('img')
	console.log($.cookie('img'))
	if(cookieImg){
    //onerror当文件加载失败时会触发这个事件
        var header = '<img src="' + cookieImg + '" onerror="this.src=\'images/user.png\'">'
        $('#user').empty().html(header + cookieName)
    }

    //退出删除cookie
    $('.dropdown-menu li').last().click(function(){
        $.removeCookie('username')
        $.removeCookie('img')
        $.get('/back',function(data){
			//console.log(data)
    		if (data.code == 'success') {
    			location.href = '/'
    		}
    	})
    })

    $('#user').click(function(){
        if(!cookieName){
           $(this).removeAttr('data-toggle')
           location.href = '/sign'
        }
    })

    $('form').submit(function(ev){
    	var valH = $('input[name=header]').val()
    	var img = ['jpg','gif','png']
    	valH = valH.split('.')
    	valH = valH[1]
    	var boo = false
		//console.log(valH)
    	console.log(img)
    	img.forEach(function(ele,num){
    		if (ele == valH) {
    			boo = true
    		}
    	})
        ev.preventDefault()
        var data = new FormData(this)
		if (boo) {
			$.post({
	            url:'/header',
	            data:data,
	            contentType:false,
	            processData:false,
	            success:function(data){
	               if(data.code == 'success'){
	                 location.href = '/'
	               }
	            }
	        })
		}else{
			alert('请输入"jpg","png","gif"格式的图片')
		}
    })
})
